using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.ObjectPool;

namespace ObjectPoolDemo.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ObjectPool<Connection> _objectPool;
        public Connection Connection { get; set; }

        // get pool from DI.
        public IndexModel(ObjectPool<Connection> objectPool)
        {
            _objectPool = objectPool;
        }

        public void OnGet()
        {
            // get object from the pool.
            Connection = _objectPool.Get();

            //remember: return to pool
            _objectPool.Return(Connection);
        }
    }
}
